package com.neusoft.utils.web;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Writer;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.neusoft.utils.json.JsonUtil;
import com.neusoft.utils.tpl.Freemarker;

/**
 * HttpServletResponse帮助类
 */
public final class ResponseUtils {
	   private static final   Log log = LogFactory.getLog(ResponseUtils.class);

	/**
	 * 发送文本。使用UTF-8编码。
	 * 
	 * @param response
	 *            HttpServletResponse
	 * @param text
	 *            发送的字符串
	 */
	public static void renderText(HttpServletResponse response, String text) {
		render(response, "text/plain;charset=UTF-8", text);
	}
	public static void renderJs(HttpServletResponse response, String text) {
		render(response, "application/x-javascript;charset=UTF-8", text);
	}
	public static void renderCss(HttpServletResponse response, String text) {
		render(response, "text/css;charset=UTF-8", text);//;charset=UTF-8
	}

	/**
	 * 发送json。使用UTF-8编码。
	 * 
	 * @param response
	 *            HttpServletResponse
	 * @param text
	 *            发送的字符串
	 */
	public static void renderJson(HttpServletResponse response, String text) {
		render(response, "application/json;charset=UTF-8", text);
	}
	public static void renderJson(HttpServletResponse response, Object obj) {
		render(response, "application/json;charset=UTF-8", JsonUtil.toJson(obj));
	}
	/**
	 * 发送xml。使用UTF-8编码。
	 * 
	 * @param response
	 *            HttpServletResponse
	 * @param text
	 *            发送的字符串
	 */
	public static void renderXml(HttpServletResponse response, String text) {
		render(response, "text/xml;charset=UTF-8", text);
	}

	public static void renderHtml(HttpServletResponse response, String text) {
		render(response, "text/html;charset=UTF-8", text);
	}
	public static String generateTpl(HttpServletResponse response,Map<String,Object> model,String url){
		Freemarker freemarker = new Freemarker();
		String resultStr = freemarker.generateString(model, 
				new File(RequestUtils.getWebRealPath("/")), url);
		return resultStr;
	}

	/**
	 * 发送内容。使用UTF-8编码。
	 * 
	 * @param response
	 * @param contentType
	 * @param text
	 */
	public static void render(HttpServletResponse response, String contentType, String text) {
		response.setContentType(contentType);
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		Writer write =null;
		try {  
			 write =response.getWriter();
			 write.write(text) ;	
			 write.close();
		} catch (IOException e) {
			log.error(e.getMessage(), e);
		}
	}
    public static void sendRedirect(HttpServletRequest request, HttpServletResponse response,String url){
    	try {
			response.sendRedirect( RequestUtils.getContextPath()+"/"+url);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
    public static void sendNotFound(HttpServletResponse response){
    	  try {
			response.sendError(HttpServletResponse.SC_NOT_FOUND);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
    public static void sendForbidden(HttpServletResponse response){
  	  try {
			response.sendError(HttpServletResponse.SC_FORBIDDEN);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
  }
	public static HttpServletResponse downloadZip(String path, HttpServletResponse response) {
		File file = new File(path);
		try {
			// 以流的形式下载文件。
			InputStream fis = new BufferedInputStream(new FileInputStream(path));
			byte[] buffer = new byte[fis.available()];
			fis.read(buffer);
			fis.close();
			// 清空response
			
			//response.getWriter().close();
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			response.setContentType("application/octet-stream");
			response.setHeader("Content-Disposition", "attachment;filename=" + new String(file.getName().getBytes("utf-8"),"iso8859-1"));
			response.setHeader("Pragma", "No-cache");
			response.setHeader("Cache-Control", "no-cache");
			response.setDateHeader("Expires", 0);
			//toClient.write("呜呜呜".getBytes());
			toClient.write(buffer);
			toClient.flush();
			toClient.close();
		} catch (IOException ex) {
			ex.printStackTrace();
		} finally {
			try {
				File f = new File(file.getPath());
				f.delete();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return response;
	}
}
